package tfc_editor.GUI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

import tfc_editor.pastrytools.PastryManager;
import tfc_editor.userManagement.UserData;
import tfc_editor.userManagement.UserManagementException;
import tfc_editor.userManagement.UserUtils;

public class FinestraLogIn extends FinestraTFC implements ActionListener
{
	private static final long serialVersionUID = 1L;
	PastryManager pm;
	EditorGUI parent;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JTextField jTextField1;

    /**
     * Constructor de la clase.
     * @param PM Punter a la classe PastryManager.
     * @param Parent Punter al editor pare.
     */
    public FinestraLogIn(PastryManager PM,EditorGUI Parent)
    {
    	super(Parent);
    	super.Entrar();
    	pm = PM;
    	parent = Parent;
        initComponents();
    }

    /**
     * Funcio que inicialitza els components de la finestra.
     */
    private void initComponents()
    {
        jLabel1 = new javax.swing.JLabel();
        jPasswordField1 = new javax.swing.JPasswordField();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jTextField1.addKeyListener(new java.awt.event.KeyAdapter()
        {
            public void keyReleased(java.awt.event.KeyEvent evt)
            {
            }
            public void keyPressed(java.awt.event.KeyEvent evt)
            {
            }
            public void keyTyped(java.awt.event.KeyEvent evt)
            {
            	if (jTextField1.getText().length()>UserUtils.LOGIN_MAX_LEN)
            	{
            		jTextField1.setText(jTextField1.getText().substring(0,UserUtils.LOGIN_MAX_LEN));
            	}
            }
        });        

        jPasswordField1.addKeyListener(new java.awt.event.KeyAdapter()
        {
            public void keyReleased(java.awt.event.KeyEvent evt)
            {
            }
            public void keyPressed(java.awt.event.KeyEvent evt)
            {
            }
            public void keyTyped(java.awt.event.KeyEvent evt)
            {
            	String psw = new String(jPasswordField1.getPassword()); 
            	if (psw.length()>UserUtils.LOGIN_MAX_LEN)
            	{
            		jPasswordField1.setText(psw.substring(0,UserUtils.PASSWORD_MAX_LEN));
            	}
            }
        });

        setTitle("Log In");
        jLabel1.setText("Usuari:");
        jLabel2.setText("Paraula clau:");
        jButton1.setText("Acceptar");
        jButton2.setText("Cancelar");

        jPasswordField1.addActionListener(this);
        jButton2.addActionListener(this);
        jButton1.addActionListener(this);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addGap(31, 31, 31)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton2)))
                .addContainerGap(31, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 22, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap())
        );

        pack();
        InitC();
    }

    /**
     * Funcio per a ser utilitzada per les classes de test que fa un login d'un usuari.
     * @param login Login desitjat.
     * @param password Password desitjat.
     */
    public void TestSetLoginIAccepta(String login,String password)
    {
    	jTextField1.setText(login);
    	jPasswordField1.setText(password);
    	ButtonAcceptarPressed();
    }
    
    /**
     * Funcio que es crida quan l'usuari pica el boto d'acceptar.
     */
    public void ButtonAcceptarPressed()
    {
		UserData ud=new UserData(jTextField1.getText(),new String(jPasswordField1.getPassword()));
		UserUtils uu = new UserUtils(pm);
		try
		{
			if (uu.ValidateUserData(ud))
			{
				uu.SetActiveUser(ud);
				JOptionPane.showMessageDialog(this,"L'usuari és correcte. L'usuari actiu ara és: "
						+ UserUtils.ActiveUser.GetLogin(),"Usuari logejat",JOptionPane.INFORMATION_MESSAGE);
				parent.ActivateOnlineMenus();
				super.Sortir();
				return;
			}
		} catch (UserManagementException e)
		{
				JOptionPane.showMessageDialog(this,"El usuari i/o el password són incorrectes");
		}
		super.Sortir();
     }

    /**
     * Funcio que escolta els events dels nodes GUI que tenim registrats per aquesta escolta.
     * @param arg0 Event que ha generat l'accio.
     */
	public void actionPerformed(ActionEvent arg0)
	{
		if (arg0.getSource()==jButton1)
    	{
			ButtonAcceptarPressed();
    	}
		else if (arg0.getSource()==jButton2)
		{
			super.Sortir();
		}
	}
}
